package com.ld.security.common.core.util;

import org.springframework.core.ResolvableType;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class GenericUtil {

	/**
	 * 获得制定类的泛型参数
	 * @param from 泛型子类
	 * @param to 泛型父类
	 * @param index 泛型参数下标
	 * @return
	 */
	public static Class<?> getGeneric(Class<?> from,Class<?> to,int... index){
		ResolvableType  rt=ResolvableType.forClass(from);
		return rt.as(to).getGeneric(index).resolve();  
	}
	
	//字段时泛型字段
	public static Class<?> getGeneric(Field field,int... index){
		//化为泛型集合
		ResolvableType resolvableType =ResolvableType.forField(field);
		return resolvableType.getGeneric(index).resolve();
	}
	
	public static Class<?> getFieldGeneric(Field field,int... index){
		ResolvableType  resolvableType = ResolvableType.forField(field);
		return resolvableType.getGeneric(index).resolve();
	}
	public static Class<?> getMethodGeneric(Method method, int... index){
		ResolvableType  resolvableType = ResolvableType.forMethodReturnType(method);
		return resolvableType.getGeneric(index).resolve();
	}

	public  static void main(String[] args){

	}
}
